# Contributor: wener <wenermail@gmail.com>
# Maintainer: wener <wenermail@gmail.com>
pkgname=fluent-bit
pkgver=3.1.9
pkgrel=0
pkgdesc="Fast and Lightweight Log processor and forwarder"
url="https://fluentbit.io/"
# riscv64, loongarch64: does not support bundled luajit
# arm: doesn't build
arch="all !armhf !armv7 !ppc64le !s390x !riscv64 !loongarch64"
license="Apache-2.0"
# XXX:
# wamr, sqlite, rdkafka, c-ares, luajit are bundled
makedepends="
	bison
	cmake
	flex
	gtest-dev
	linux-headers
	musl-fts-dev
	openssl-dev
	yaml-dev
	zlib-dev
	"
subpackages="
	$pkgname-dev
	$pkgname-openrc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/fluent/fluent-bit/archive/v$pkgver.tar.gz
	fluent-bit.initd
	chunkio-static-lib-fts.patch
	exclude-luajit.patch
	musl-strerror_r.patch
	"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	# default CORE_STACK_SIZE=((3 * PTHREAD_STACK_MIN) / 2)=3072 is invalid
	# based on https://docs.fluentbit.io/manual/administration/configuring-fluent-bit/classic-mode/configuration-file
	# set default to 24576
	#
	# Skipped some tests: -DFLB_TESTS_RUNTIME not enabled, multiple failed tests
	#  1 - flb-it-pack: failed on aarch64
	# 14 - flb-it-utils, 53 - flb-it-aws_util,
	# 59 - flb-it-aws_credentials_process: failed on all enabled arches
	cmake -B build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=None \
		-DFLB_CORO_STACK_SIZE=24576 \
		-DFLB_TESTS_INTERNAL=Yes \
		-DFLB_WITHOUT_flb-it-pack=Yes \
		-DFLB_WITHOUT_flb-it-utils=Yes \
		-DFLB_WITHOUT_flb-it-aws_util=Yes \
		-DFLB_WITHOUT_flb-it-aws_credentials_process=Yes \
		-DFLB_TLS=Yes \
		-DFLB_HTTP_SERVER=Yes \
		$CMAKE_CROSSOPTS .
	make -C build
}

check() {
	CTEST_OUTPUT_ON_FAILURE=1 make -C build test
}

package() {
	make -C build DESTDIR="$pkgdir" install

	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname

	mv "$pkgdir"/usr/etc/* "$pkgdir"/etc
}

sha512sums="
fbf6a8c8b35cafb8fc2f95e5a52dee835c86af9f1bcded9edabca9586c41cf9c4ded776018dfecc0f96b7f7a68587f263fb19704c458f8606b6b1033a323034e  fluent-bit-3.1.9.tar.gz
e17c8b7d09b524855167baf3a3766c5420181370cdd1d21133e7c18d7316b224a6c0aa240be26d3d0443d91247a82759161f7ad12f142b4b84924da5694a61e6  fluent-bit.initd
6bd7d8b4da93a17f29b6ea1e0286ea226d0e376024284741110936779b3229bd8d6cd03ffbdc5d3b4842294e7f32a888de0dd16b0851b65d91b062ca58530ea0  chunkio-static-lib-fts.patch
aa1449f8f4599330e1a01c2837f22cb1d10ce5926d9cb92f92fb89939c7b6af3116d3339566709ad466ebf6e37a5b178e84619ec6a1608327241a0374992be81  exclude-luajit.patch
f11e4e6f674e2e7d56f3b77e1f6f0afbf1d15004f5d43d91080697ec39ef53d330565c46b02cb5d97612450fe0cc0a9e9a4c895c0fe021057a7cc2b6e62557bc  musl-strerror_r.patch
"
